package MyCustomTable;

import MyUtil.DataBaseUtil;
import MyUtil.MyPanel;

import javax.swing.*;
import java.awt.*;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Book_add extends JFrame{
    // 数据库相关
    private ResultSet rs;
    private String sql1="insert into Book values(?,?,?,?,?,?,?,?)";
    private String sql2="insert into Book(Bid,Bname,Bpay,Bauthor,Btype,Bpress,Bnumber) values(?,?,?,?,?,?,?)";
    private String sql3 = "select max(Bid) from Book";
    private String sql4 = "select Bid from Book where Bname = ? and Bauthor = ? and Btype =? and Bpress =?";
    private String sql5 = "Update Book set Bnumber = Bnumber+?,Bpay = ? where Bid = ?";

    private String eid = null;
    private int eid_int = -1;

    //建立二个控件

    private JPanel jpanel1 = new MyPanel("/picture/3.jpg");

    //空间二的内容
    //人名
    JLabel jc1  = new JLabel("图书姓名");
    JTextField jt1 = new JTextField(10);
    //账号
    JLabel jc2 = new JLabel("图书单价");
    JTextField jt2 = new JTextField(10);
    //密码
    JLabel jc3 = new JLabel("图书作者");
    JTextField jt3 = new JTextField(10);
    //等级
    JLabel jc4 = new JLabel("图书类别");
    JTextField jt4 = new JTextField(11);
    //电话
    JLabel jc5 = new JLabel("出版社");
    JTextField jt5 = new JTextField(11);
    //薪水
    JLabel jc6 = new JLabel("入库数量");
    JTextField jt6 = new JTextField(10);
    //备/注
    JCheckBox jc7 = new JCheckBox("备注（不输入时,不开即可）");
    JTextField jt7 = new JTextField(10);
    //按钮
    JButton jb1 = new JButton("添加");

    public Book_add() throws SQLException {
        //设置总JFrame属性
        super("图书入库");
        setBounds(400,200,800,700);
        setContentPane(jpanel1);
        this.setVisible(true);
        //设置控件二
        jpanel1.add(jc1);
        jpanel1.add(jt1);
        jpanel1.add(jc2);
        jpanel1.add(jt2);
        jpanel1.add(jc3);
        jpanel1.add(jt3);
        jpanel1.add(jc4);
        jpanel1.add(jt4);
        jpanel1.add(jc5);
        jpanel1.add(jt5);
        jpanel1.add(jc6);
        jpanel1.add(jt6);
        jpanel1.add(jc7);
        jpanel1.add(jt7);
        jpanel1.add(jb1);
        jt1.setText("");
        jt2.setText("");
        jt3.setText("");
        jt4.setText("");
        jt5.setText("");
        jt6.setText("");
        jt7.setText("");
        jpanel1.setLayout(new CustomLayout());
        eid = getFirstEmployeeEid();
        jb1.addActionListener((e)->{
            if(jc7.isSelected()) {
                if ((jt1.getText()).equals("")){
                    JOptionPane.showMessageDialog(this,"有项目缺少数据!");
                }else if ((jt2.getText()).equals("")){
                    JOptionPane.showMessageDialog(this,"有项目缺少数据!");
                }else if (jt3.getText().equals("")){
                    JOptionPane.showMessageDialog(this,"有项目缺少数据!");;
                }else if ((jt4.getText()).equals("")){
                    JOptionPane.showMessageDialog(this,"有项目缺少数据!");
                }else if ((jt5.getText()).equals("")){
                    JOptionPane.showMessageDialog(this,"有项目缺少数据!");
                }else{
                    //插入信息
                    String Bid = null;
                    //查询是否为同一本书
                    Object[] object = new Object[4];
                    object[0] = jt1.getText();
                    object[1] = jt3.getText();
                    object[2] = jt4.getText();
                    object[3] = jt5.getText();
                    try {
                        rs = DataBaseUtil.prepareResultSet(sql4,object);
                        while(rs.next()){
                            Bid = rs.getString(1);
                        }
                        if(Bid !=null){
                            Object[] o  = new Object[3];
                            o[1] = Float.parseFloat(jt2.getText());
                            o[2] = Bid;
                            o[0] = Integer.parseInt(jt6.getText());
                            DataBaseUtil.changeResultSet(sql5,o);
                        }
                        else{
                            Object[] objects = new Object[8];
                            objects[0] = eid;
                            objects[1] = jt1.getText();
                            objects[2] = Float.parseFloat(jt2.getText());
                            objects[3] = jt3.getText();
                            objects[4] = jt4.getText();
                            objects[5] = jt5.getText();
                            objects[6] = Integer.parseInt(jt6.getText());
                            objects[7] = jt7.getText();
                            try {
                                DataBaseUtil.changeResultSet(sql1,objects);
                            } catch (SQLException ex) {
                                ex.printStackTrace();
                            }
                            eid = getNextEmployeeEid();
                        }
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }
            }
            else{
                if ((jt1.getText()).equals("")){
                    JOptionPane.showMessageDialog(this,"有项目缺少数据!");
                }else if ((jt2.getText()).equals("")){
                    JOptionPane.showMessageDialog(this,"有项目缺少数据!");
                }else if (jt3.getText().equals("")){
                    JOptionPane.showMessageDialog(this,"有项目缺少数据!");
                }else if ((jt4.getText()).equals(""))
                {
                    JOptionPane.showMessageDialog(this,"有项目缺少数据或不符合要求!");
                }else if ((jt5.getText()).equals("")){
                    JOptionPane.showMessageDialog(this,"有项目缺少数据!");
                }else{
                    //插入信息
                    String Bid = null;
                    //查询是否为同一本书
                    Object[] object = new Object[4];
                    object[0] = jt1.getText();
                    object[1] = jt3.getText();
                    object[2] = jt4.getText();
                    object[3] = jt5.getText();
                    try {
                        rs = DataBaseUtil.prepareResultSet(sql4,object);
                        while(rs.next()){
                            Bid = rs.getString(1);
                        }
                        if(Bid !=null){
                            Object[] o  = new Object[3];
                            o[1] = Float.parseFloat(jt2.getText());
                            o[2] = Bid;
                            o[0] = Integer.parseInt(jt6.getText());
                            DataBaseUtil.changeResultSet(sql5,o);
                        }
                        else{
                            Object[] objects = new Object[7];
                            objects[0] = eid;
                            objects[1] = jt1.getText();
                            objects[2] = Float.parseFloat(jt2.getText());
                            objects[3] = jt3.getText();
                            objects[4] = jt4.getText();
                            objects[5] = jt5.getText();
                            objects[6] = Integer.parseInt(jt6.getText());
                            try {
                                DataBaseUtil.changeResultSet(sql2,objects);
                            } catch (SQLException ex) {
                                ex.printStackTrace();
                            }
                            eid = getNextEmployeeEid();
                        }
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });

    }
    public String getFirstEmployeeEid() throws SQLException {
        String d1 = null;
        rs = DataBaseUtil.produceResultSet(sql3);
        while(rs.next()){
            d1 = rs.getString(1);
        }
        char[] ar = "00000000".toCharArray();
        int n = Integer.parseInt(d1.replaceFirst("bk",""))+1;
        eid_int = n;
        int number =0;
        while(n>0){
            ar[7-number]= ((char)(n%10+48));
            n/=10;
            number++;
        }
        String n1 = new String(ar);
        return "bk"+n1;
    }
    public String getNextEmployeeEid(){
        if(eid_int!=-1) {
            char[] ar = "00000000".toCharArray();
            eid_int++;
            int number = 0;
            int n = eid_int;
            while (n > 0) {
                ar[7 - number] = ((char) (n%10+48));
                n /= 10;
                number++;
            }
            String n1 = new String(ar);
            return "bk" + n1;
        }
        return null;
    }
    private class CustomLayout implements  LayoutManager{

        @Override
        public void addLayoutComponent(String name, Component comp) {

        }

        @Override
        public void removeLayoutComponent(Component comp) {

        }

        @Override
        public Dimension preferredLayoutSize(Container parent) {
            return null;
        }

        @Override
        public Dimension minimumLayoutSize(Container parent) {
            return null;
        }

        @Override
        public void layoutContainer(Container parent) {
            int width = getWidth();
            int height = getHeight();
            if(jc1.isVisible()){
                jc1.setBounds(width/8, height/8,60,30);
            }
            if(jt1.isVisible()){
                jt1.setBounds(width/8+60, height/8,100,25);
            }
            if(jc2.isVisible()){
                jc2.setBounds(width/8, height/8+25,60,25);
            }
            if(jt2.isVisible()){
                jt2.setBounds(width/8+60, height/8+25,100,25);
            }
            if(jc3.isVisible()){
                jc3.setBounds(width/8, height/8+50,60,25);
            }
            if(jt3.isVisible()) {
                jt3.setBounds(width / 8 + 60, height / 8 + 50, 100, 25);
            }
            if(jc4.isVisible()){
                jc4.setBounds(width/8,height/8+75,60,25);
            }
            if(jt4.isVisible()){
                jt4.setBounds(width/8+60, height/8+75,100,25);
            }
            if(jc5.isVisible()){
                jc5.setBounds(width/2,height/8,60,25);
            }
            if (jt5.isVisible()){
                jt5.setBounds(width/2+60,height/8,100,25);
            }
            if(jc6.isVisible()){
                jc6.setBounds(width/2,height/6,60,25);
            }
            if (jt6.isVisible()){
                jt6.setBounds(width/2+60,height/6,100,25);
            }
            if(jc7.isVisible()){
                jc7.setBounds(width/2,height/6+30,60,25);
            }
            if (jt7.isVisible()){
                jt7.setBounds(width/2+60,height/6+30,100,25);
            }
            if(jb1.isVisible()) {
                jb1.setBounds(7 * width / 8 - 70, height / 11, 70, height / 5);
            }
        }
    }
}